/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.transitionseverywhere;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.utils.ObjectAttribute;
/**
 * This transition tracks changes during scene changes to the
 * {@link View#setBackground(ohos.agp.components.element.Element) background}
 * property of its target views (when the background is a
 * {@link ColorDrawable}, as well as the
 * {@link TextView#setTextColor(ohos.agp.colors.RgbColor)
 * color} of the text for target TextViews. If the color changes between
 * scenes, the color change is animated.
 */
public class Recolor extends Transition {

    private static final String PROPNAME_BACKGROUND = "harmony:recolor:background";
    private static final String PROPNAME_TEXT_COLOR = "harmony:recolor:textColor";

    public static ObjectAttribute<Text, Integer> TEXTVIEW_TEXT_COLOR;
    public static  ObjectAttribute<ShapeElement, Integer> COLORDRAWABLE_COLOR;



    public Recolor() {

    }

    public Recolor(Context context, AttrSet attrs) {
        super(context, attrs);
    }
    private void captureValues(TransitionValues transitionValues) {
        transitionValues.values.put(PROPNAME_BACKGROUND, transitionValues.view.getBackgroundElement());
        if (transitionValues.view instanceof Text) {
            transitionValues.values.put(PROPNAME_TEXT_COLOR,
                    ((Text) transitionValues.view).getTextColor());
        }
    }

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }


    public Animator createAnimator(ComponentContainer sceneRoot, TransitionValues startValues,
                                   TransitionValues endValues) {

        if (startValues == null || endValues == null) {
            return null;
        }
        final Component view = endValues.view;
        Element startBackground = (Element) startValues.values.get(PROPNAME_BACKGROUND);
        Element endBackground = (Element) endValues.values.get(PROPNAME_BACKGROUND);
        AnimatorProperty bgAnimator = null;
        if (startBackground instanceof ShapeElement && endBackground instanceof ShapeElement) {
            ShapeElement startColor = (ShapeElement) startBackground;
            ShapeElement endColor = (ShapeElement) endBackground;
            if (startColor.getRgbColors() != endColor.getRgbColors()) {
                final RgbColor[] finalColor = endColor.getRgbColors();
            }
        }
        AnimatorProperty textColorAnimator = null;
        if (view instanceof Text) {
            Text textView = (Text) view;
            int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
            int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
        }
        return TransitionUtils.mergeAnimators(bgAnimator, textColorAnimator);
    }
}
